package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Dish;
import com.example.domain.Setmeal;
import com.example.dto.DishDto;
import com.example.dto.SetmealDto;
import com.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页、查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> getSetmealPage(@PathParam("page") Integer page, @PathParam("pageSize") Integer pageSize, @PathParam("name") String name){
        //查询数据库数据
        Page<Setmeal> pageInfo = setmealService.getSetMealListForPage(page, pageSize, name);
        //映射
        Page<SetmealDto> setmealDtoPage = setmealService.setMealToSetMealDto(pageInfo);

        return R.success(setmealDtoPage);
    }

    /**
     * 更改菜品是否起售状态
     * @param status
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value ="setmealCache",allEntries = true)//清理setmealCache下所有的缓存数据
    public R<String> setmealStatusByStatus(@PathVariable Integer status, @PathParam("ids") Long[] ids, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            setmealService.setMealStatusByStatus(status,ids,session);
            return R.success("更改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("更改失败");
        }
    }

    /**
     * 添加套餐
     * @return
     */
    @PostMapping
    @Transactional
    @CacheEvict(value ="setmealCache",allEntries = true)//清理setmealCache下所有的缓存数据
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithFlavor(setmealDto);
        return R.success("新增菜品成功");
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> querySetmealById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    /**
     * 修改菜品
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value ="setmealCache",allEntries = true)//清理setmealCache下所有的缓存数据
    public R<String> update(@RequestBody SetmealDto  setmealDto) {
        setmealService.updateWithFlavor(setmealDto);
        return R.success("新增菜品成功");
    }

    /**
     * 删除单个或者多个
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional
    @CacheEvict(value ="setmealCache",allEntries = true)//清理setmealCache下所有的缓存数据
    public R<String> deleteSetmeal(Long[] ids) {
        try {
            boolean b = setmealService.deleteByIds(ids);
            if (b) {
                return R.success("删除成功");
            }
            return R.error("由于套餐为起售状态，需要为停售状态才能删除");
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("删除失败");
        }
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status",unless = "#result==null")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
